<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
</body>
<script>
    // 点击某个按钮, 提示"点击的是第n个按钮"
    const btns = document.querySelectorAll("button");
    // for(var i=0;i<btns.length;i++){
    //     btns[i].onclick = (function(i){
    //         return function(){
    //             console.log("fn",i)
    //         }
    //     })(i);
    // }


    // 相当于:
    var i = 0;
    btns[i].onclick = (function (i) {
        return function () {
            console.log(i);
        }
    })(i);
    i++;
    btns[i].onclick = (function (i) {
        return function () {
            console.log(i);
        }
    })(i);
    i++;
    btns[i].onclick = (function (i) {
        return function () {
            console.log(i);
        }
    })(i);
    i++;
    btns[i].onclick = (function (i) {
        return function () {
            console.log(i);
        }
    })(i);
    i++;
    btns[i].onclick = (function (i) {
        return function () {
            console.log(i);
        }
    })(i);
    i++;
    console.log(i);

</script>
</html>